package mysql

import (
	"furni-set/furnirpc/furnirpc"
	"gorm.io/gorm"
)

type House struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(30);not null;commit:家具名称"`
	Price   float64 `gorm:"type:decimal(10,2);not null;commit:家具价格"`
	Stock   int64   `gorm:"type:int(11);not null;commit:家具库存"`
	Image   string  `gorm:"type:varchar(30);not null;commit:家具图片"`
	BrandId int64   `gorm:"type:int(11);not null;commit:品牌id"`
	ClassId int64   `gorm:"type:int(11);not null;commit:分类id"`
}

func (h *House) HouseAdd(db *gorm.DB) error {
	return db.Debug().Create(h).Error
}

func HouseShow(db *gorm.DB, show []*furnirpc.HouseList, id int64) []*furnirpc.HouseList {
	db.Debug().Model(&House{}).
		Select(
			"houses.id as house_id",
			"houses.name as name",
			"houses.price as price",
			"houses.stock as stock",
			"houses.image as image",
			"brands.name as brand_name",
			"classes.name as class_name",
		).
		Joins("left join classes on classes.id=houses.class_id").
		Joins("left join brands on brands.id=houses.brand_id").
		Where("houses.id = ?", id).First(&show)
	return show
}

func HouseList(db *gorm.DB, list []*furnirpc.HouseList) []*furnirpc.HouseList {
	db.Debug().Model(&House{}).
		Select(
			"houses.id as house_id",
			"houses.name as name",
			"houses.price as price",
			"houses.stock as stock",
			"houses.image as image",
			"brands.name as brand_name",
			"classes.name as class_name",
		).
		Joins("left join classes on classes.id=houses.class_id").
		Joins("left join brands on brands.id=houses.brand_id").
		Find(&list)
	return list
}
